package br.com.linkers.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.linkers.data.HibernateUtil;
import br.com.linkers.entity.Emails;
import br.com.linkers.entity.Telefones;
import br.com.linkers.entity.Usuarios;

public class EmailsDAO {

	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();

	List<Emails> lista;

	public void newSession() {
		this.session = HibernateUtil.getSessionFactory().openSession();
		this.transaction = session.beginTransaction();
	}

	public void inserir(Emails email) {

		newSession();

		session.persist(email);

		transaction.commit();
		session.close();
	}

	public void deletar(Emails email) {

		newSession();

		session.delete(email);

		transaction.commit();
		session.close();
	}

	public void atualizar(Emails email) {
		session.update(email);
	}

	public ArrayList<Emails> buscaPorIdUsuario(Long id){
		newSession();
		Usuarios u = new Usuarios();
		u.setId(id);
		Criteria cri = session.createCriteria(Emails.class)
				.add(Restrictions.eq("usuario", u));
		return (ArrayList<Emails>) cri.list();
	}

	public List<Emails> listar() {
		newSession();
		Criteria cri = session.createCriteria(Emails.class);
		this.lista = cri.list();
		return this.lista;
	}
}
